package fun.yyds.service.file.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.github.tobato.fastdfs.service.GenerateStorageClient;
import fun.yyds.common.ResponseException;
import fun.yyds.common.constant.RespCodeEnum;
import fun.yyds.entity.file.FileEntity;
import fun.yyds.mapper.mysql.FileMapper;
import fun.yyds.service.file.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: FileServiceImpl
 * @author: Chensn
 * @time: 2024/10/31
 */
@Service
@RequiredArgsConstructor
public class FileServiceImpl extends ServiceImpl<FileMapper, FileEntity> implements FileService {

    private final FastFileStorageClient fastFileStorageClient;
    @Resource(name = "defaultGenerateStorageClient")
    private GenerateStorageClient generateStorageClient;
    private static final String UNIT_K = "k";
    @Resource
    private FileMapper fileMapper;

    public List<FileEntity> fileUpload(List<MultipartFile> files) {
        List<FileEntity> fileEntityList = new ArrayList<>();

        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
//            Set<MetaData> metaDataSet = new HashSet<>();
//            MetaData metaData = new MetaData();
//            metaData.setValue("Copyright yyds6");
//            metaDataSet.add(metaData);
            if (StringUtils.isBlank(fileName)) {
                throw new ResponseException(RespCodeEnum.FILE_PARSING_FAILED);
            }
            String originalFilename = file.getOriginalFilename();
            long size = file.getSize();
            //上传文件
//                StorePath storePath = fastFileStorageClient.uploadFile(mysql.getInputStream(), size, FilenameUtils.getExtension(originalFilename), null);
            //保存上传记录
            FileEntity uploadEntity = FileEntity.builder()
//                        .url(storePath.getFullPath())
                    .url("test")
                    .name(originalFilename)
                    .type(file.getContentType())
                    .size(size + UNIT_K)
                    .build();
//            fileMapper.insert(uploadEntity);
            fileMapper.saveFileInfo(uploadEntity);
            fileEntityList.add(uploadEntity);
        }
        return fileEntityList;
    }

    @Override
    public List<FileEntity> fileList() {
        return null;
    }

    @Override
    public List<FileEntity> fileDownload() {
        return null;
    }

    @Override
    public int fileDelete() {
        return 0;
    }
}
